﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using UnityEngine;

public class SocketFace : MonoBehaviour {
    const int portNo = 500;
    private TcpClient _client;
    byte[] data;
    ByteBuffer bbf;//接受到的包
    public static SocketFace instance;

    public ConveyorBelt con1;
    public ConveyorBelt con2;

    public Rod bas;
    public Rod hor;
    public Rod ver;
    public Rod buid;
    public Hold hold;
    public Build build;






    private void Awake()
    {
        instance = this;
        try
        {
            this._client = new TcpClient();
            this._client.Connect("127.0.0.1", portNo);
            data = new byte[this._client.ReceiveBufferSize];
            //SendMessage(txtNick.Text);
            this._client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
        }
        catch (Exception ex)
        {
            print(ex.Message);
        }
    }

   

  

    public new void SendMessage(string message)
    {
        try
        {
            NetworkStream ns = this._client.GetStream();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
            ns.Write(data, 0, data.Length);
            ns.Flush();
        }
        catch (Exception ex)
        {
            print(ex.Message);
        }
    }

    //发送一个数据包
    public void SendBuf(ByteBuffer bf)
    {
      //  print("send");
            NetworkStream ns = this._client.GetStream();
        //print(bf.Capacity());
             ns.Write(bf.ToArray(), 0,bf.Capacity() );
            ns.Flush();
        
       
    }


        //在头部写入一个short
    public static byte[] HeadByte(ByteBuffer bf)
    {
        byte[] data = bf.ToArray();
        byte[] dat = new byte[data.Length + 2];
        Array.Copy(data, 0, dat, 2, data.Length);
        Array.Copy(BitConverter.GetBytes((short)data.Length), 0, dat, 0, 2);
        return dat;
    }

  

    

    public void ReceiveMessage(IAsyncResult ar)
    {
       
   
            int bytesRead;
            bytesRead = this._client.GetStream().EndRead(ar);
            if (bytesRead < 1)
            {
                return;
            }
            else
            {
              //  Debug.Log(System.Text.Encoding.ASCII.GetString(data, 0, bytesRead));
                string message = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
                bbf = ByteBuffer.Allocate(bytesRead);
                bbf.WriteBytes(data,0, bytesRead) ;

               // print(bbf.ReadUTF()+":"+bbf.ReadInt());
              //  SendMessage("haha");
                DealRec(bbf);
            }
            this._client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
    
    }

    private void Update()
    {
        
    }


    



    //处理接受的信息
    void DealRec(ByteBuffer bbf  )
    {
       int lenght = bbf.ReadShort();
       // print(lenght);

        string fc = bbf.ReadUTF();
       // print(fc);
        switch (fc)
        {
            case "conveyor1":
                float v = bbf.ReadFloat();
                con1.SetSpeed(v);
                break;
            case "conveyor2":
                float v2 = bbf.ReadFloat();
                con2.SetSpeed(v2);
                break;
            case "moveb":
                SetTagertByBff(bbf, bas);
                break;
            case "horizontal":
                SetTagertByBff(bbf, hor);
                break;
            case "vertical":
                SetTagertByBff(bbf, ver);
                break;
            case "setcolor":
                build.SetColor(bbf.ReadInt());
                break;
            case "gethold":
                hold.tagHolding=true;
                break;
            case "release":
                hold.tagHolding = false ;
                break;
            case "buildworkpiece":
                build.needBuild++;
                break;
            default:
                break;
        }
    }


    private void SetTagertByBff(ByteBuffer bf,Rod r)
    {
        float bx = bf.ReadFloat();
        float by = bf.ReadFloat();
        float bz = bf.ReadFloat();
        r.SetTagert(bx, by, bz);
    }


    void OnDestroy()
    {
        if (_client == null) return;
        this._client.Close();
    }
}
